﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for GeoDistance
/// </summary>
public class GeoDistance
{
	public GeoDistance()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    // This metode calculates the distance between two points (given the latitude/longitude of those points).
    // Definitions: 
    //   South latitudes are negative, east longitudes are positive
    // Passed to metode:
    //   slat, slon = Cordinates of start point (in decimal degrees)
    //   dlat, dlon = Cordinates of dest point (in decimal degrees)

    public double distance(double slat, double slon, double dlat, double dlon)
    {
        double theta = slon - dlon;
        double dist = Math.Sin(deg2rad(slat)) * Math.Sin(deg2rad(dlat)) + Math.Cos(deg2rad(slat)) * Math.Cos(deg2rad(dlat)) * Math.Cos(deg2rad(theta));
        dist = Math.Acos(dist);
        dist = rad2deg(dist);
        dist = dist * 60 * 1.1515 * 1.609344; // dist in km
        return (dist);
    }

    // Converts decimal degrees to radians
    private double deg2rad(double deg)
    {
        return (deg * Math.PI / 180.0);
    }

    // Converts radians to decimal degrees
    private double rad2deg(double rad)
    {
        return (rad / Math.PI * 180.0);
    }

}